package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存钱
 */
public class SavePanel extends JPanel {

    private JLabel input = new JLabel("输入您的存款金额：");
    private JTextField inputText = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("src/ATM/image/buttonSure.jpg"));
    private JButton endBtn = new JButton(new ImageIcon("src/ATM/image/buttonOff.jpg"));
    private Font font = new Font("宋体", Font.BOLD,25);
    public SavePanel(ATMFrame atm){
        this.setLayout(null);
        this.setOpaque(false);

        input.setBounds(300,300,250,50);
        input.setFont(font);
        inputText.setBounds(530,300,220,50);
        inputText.setFont(font);
        sureBtn.setBounds(60,600,140,50);
        sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double blance = Double.parseDouble(inputText.getText());
                    if (blance <= 0){
                        JOptionPane.showMessageDialog(SavePanel.this,"金额必须大于0");
                        return;
                    }
                    atm.getUsers().get(atm.getIndex()).setBlance(atm.getUsers().get(atm.getIndex()).getBlance() + blance);
                    FileUtil.writeObject(atm.getUsers(),atm.getFILE_PATH());
                    JOptionPane.showMessageDialog(SavePanel.this,"存款成功");
                    atm.showPanel("menu");
                }catch (Exception k){
                    JOptionPane.showMessageDialog(SavePanel.this,"金额输入有误，存款失败");
                }
            }
        });
        endBtn.setBounds(824,600,140,50);
        endBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                atm.showPanel("menu");
            }
        });

        this.add(input);
        this.add(inputText);
        this.add(sureBtn);
        this.add(endBtn);
    }

    public void Refresh(){
        inputText.setText("");
    }

}
